package com.processor;

import com.annotation.Check;
import com.google.auto.service.AutoService;

import javax.annotation.processing.*;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;
import java.util.Set;

/**
 * 检查使用注解时，使用的是否正确
 */
@SupportedSourceVersion(SourceVersion.RELEASE_8)
@SupportedAnnotationTypes({"com.annotation.Check"})
@AutoService(Processor.class)
public class CheckProcessor extends AbstractProcessor {
    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        //获取所有被Check注解的元素
        Set<? extends Element> set = roundEnv.getElementsAnnotatedWith(Check.class);
        for(Element element : set){
            Check check = element.getAnnotation(Check.class);
            String name = check.name();
            if(name == null || name.trim().length() == 0){
                this.processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,"name属性不能为空",element);
            }
        }
        return true;
    }
}
